{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "58e4f06d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['S__:6950=二七八〇二七八〇二七八〇二七八〇〇E',\n",
       " 'S__:6290=wtqywtqywtqywtqypE',\n",
       " 'S__:4457=quiwouiwouiwouiwiE',\n",
       " 'S__:9794=39179917991799176E',\n",
       " 'S__:7442=wouupouupouupouyiE',\n",
       " 'S__:3934=壹伍柒叁柒伍柒叁柒伍柒叁柒伍柒叁陆E',\n",
       " 'S__:4719=壹捌捌柒柒捌捌柒柒捌捌柒柒捌捌柒陆E',\n",
       " 'S__:8105=ewrwewrwewrwewrwpE',\n",
       " 'S__:6090=wreywreywreywreypE',\n",
       " 'S__:1125=四五〇〇四五〇〇四五〇〇四五〇〇EP']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%run common.ipynb\n",
    "\n",
    "[tokenizer.decode(i) for i in tokenizer.get_batch_data(prefix=False)[1]][:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "62aea6a1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/anaconda3/envs/pt2/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "model_cls = ModelCLS()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dbe0768c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0.203125\n",
      "S__:2924=11697169716971696E 3\n",
      "S__:7110=28442844284428440E 3\n",
      "100 1.0\n",
      "S__:3840=一五三六一五三六一五三六一五三六〇E 2\n",
      "S__:1869=7476747674767476EP 0\n",
      "200 1.0\n",
      "S__:6465=wtiywtiywtiywtiypE 1\n",
      "S__:8173=叁贰陆玖伍贰陆玖伍贰陆玖伍贰陆玖贰E 3\n",
      "300 1.0\n",
      "S__:1550=陆贰零零陆贰零零陆贰零零陆贰零零EP 3\n",
      "S__:8783=叁伍壹叁伍伍壹叁伍伍壹叁伍伍壹叁贰E 3\n",
      "400 1.0\n",
      "S__:4071=壹陆贰捌伍陆贰捌伍陆贰捌伍陆贰捌肆E 3\n",
      "S__:5895=二三五八二三五八二三五八二三五八〇E 2\n"
     ]
    }
   ],
   "source": [
    "optimizer = torch.optim.AdamW(params=model_cls.parameters(), lr=1e-4)\n",
    "criterion = torch.nn.CrossEntropyLoss()\n",
    "\n",
    "for epoch in range(500):\n",
    "    label, input_ids, attention_mask = tokenizer.get_batch_data(prefix=False)\n",
    "    label = torch.LongTensor(label).to(device)\n",
    "    input_ids = torch.LongTensor(input_ids).to(device)\n",
    "    attention_mask = torch.LongTensor(attention_mask).to(device)\n",
    "\n",
    "    logits = model_cls(input_ids=input_ids, attention_mask=attention_mask)\n",
    "\n",
    "    loss = criterion(logits, label)\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "    optimizer.zero_grad()\n",
    "\n",
    "    if epoch % 100 == 0:\n",
    "        logits = logits.argmax(1)\n",
    "        acc = (logits == label).sum().item() / len(label)\n",
    "        print(epoch, acc)\n",
    "\n",
    "        for i in range(2):\n",
    "            print(tokenizer.decode(input_ids[i].tolist()), logits[i].item())\n",
    "\n",
    "model_cls.to('cpu')\n",
    "torch.save(model_cls, 'cls.model')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
